// 区分开发环境和生产环境  接口地址
let baseUrl;

if (process.env.NODE_ENV === 'development') {
    // baseUrl = 'http://47.110.48.22/heka/test'
    baseUrl = 'https://www.1cardd.com/heka/app'    // 开发环境
} else {
    baseUrl = 'https://www.1cardd.com/heka/app'    // 生产环境
}

const whiteList = [
    '/api/v1/shop/staff/login',
]
const https = async function (config) {
    //显示loading
    wx.showLoading({
        title: '加载中'
    });
    //检查缓存中有没有token
    if (whiteList.indexOf(config.url) === -1 && !wx.getStorageSync('token')) {
        uni.redirectTo({
            url: '/pages/tabBar/my/login'
        })
        return
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request({
            url: baseUrl + config.url,
            method: config.method || 'GET',
            data: config.data || {},
            header: {
                Authorization: `Bearer ${uni.getStorageSync("token")}`,
               'X-UNI-TOKEN': uni.getStorageSync("token")
            }
        }).then(res => {
            wx.hideLoading()      //隐藏loading
            if (res[0]) {
                uni.showToast({
                    title: "数据获取失败",
                    icon: "none",
                    duration: 3000
                })
                resolve(false);
            } else {
                let data = res[1].data;
                if (data.code === 200) {
                    resolve(data);
                } else {
                    uni.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 3000
                    })
                    reject()
                }
            }
        }).catch(error => {
            wx.hideLoading()      //隐藏loading
            reject(error);
        })
    })
    return promise
}

export default https